<template>
	<view class="red-packet-container" v-if="show" @tap.stop>
		<view class="red-packet-mask" @tap="close"></view>
		<view class="red-packet-content">
			<!-- 红包封面 -->
			<view class="red-packet-cover" @click="openRedPacket" :animation="animationData" v-if="!isOpened">
				<view class="cover-top">
					<view class="cover-title"></view>
				</view>
				<view class="cover-body">
					<view class="cover-title">恭喜获得新人红包</view>
					<view class="cover-amount">5元</view>
					<view class="cover-open-btn">开</view>
					<view class="cover-tips">领取后存入余额，可立即提现</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		giveNewUserBonus
	} from "@/apis/user.js"
	export default {
		name: "RedPacketPopup",
		props: {
			show: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				isOpened: false,
				animationData: {}
			}
		},
		methods: {
			openRedPacket() {
				// 模拟开红包动画
				this.animateCover();
				uni.vibrateShort(); // 震动反馈

				// 模拟领取红包
				setTimeout(() => {
					this.isOpened = true;
					this.close(); // 领取后直接关闭

					// 这里可以添加实际领取逻辑
					this.receiveRedPacket();
				}, 1000);
			},

			animateCover() {
				// 使用uni.createAnimation
				this.animation = uni.createAnimation({
					duration: 500,
					timingFunction: 'ease'
				});

				this.animation.scale(0.9).rotate(10).step();
				this.animation.scale(1.1).rotate(-10).step();
				this.animation.scale(1).rotate(0).step();

				this.animationData = this.animation.export();

				// 2秒后重置动画
				setTimeout(() => {
					this.animationData = {};
				}, 2000);
			},

			receiveRedPacket() {
				// 实际项目中替换为真实API请求
				// uni.showToast({
				// 	title: '红包已存入余额',
				// 	icon: 'success'
				// });

				// 这里可以调用API实际领取红包
				giveNewUserBonus().then(res => {
					uni.showToast({
						title: '红包已存入余额',
						icon: 'success'
					});
				})	
			},

			close() {
				this.$emit('update:show', false);
				this.$emit('close');
				this.isOpened = false; // 重置状态
			}
		}
	}
</script>

<style scoped>
	.red-packet-container {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 999;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.red-packet-mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.7);
	}

	.red-packet-content {
		position: relative;
		z-index: 1000;
		width: 80%;
		max-width: 300px;
	}

	.red-packet-cover {
		position: relative;
		width: 100%;
		height: 360rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.cover-top {
		width: 100%;
		height: 40rpx;
		background-color: #FF4E4E;
		border-radius: 16rpx 16rpx 0 0;
	}

	.cover-body {
		width: 100%;
		flex: 1;
		background: linear-gradient(to bottom, #FF4E4E, #E4393C);
		border-radius: 0 0 16rpx 16rpx;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.cover-title {
		color: #FFF;
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
	}

	.cover-amount {
		color: #FFD700;
		font-size: 48rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
	}

	.cover-open-btn {
		width: 100rpx;
		height: 100rpx;
		background-color: #FFD700;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #B22222;
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.2);
	}

	.cover-tips {
		color: rgba(255, 255, 255, 0.8);
		font-size: 24rpx;
	}
</style>